from django.shortcuts import render
from data_collector.appstore_crawler import AppstoreCrawler
from data_collector.douban_crawler import DoubanCrawler
from data_collector.news_crawler import NewsCrawler
from data_collector.weather_crawler import WeatherCrawler
from data_collector.weibo_crawler import WeiboCrawler
from data_collector.stock_crawler import StockCrawler
from BigDataWeb.view import get_algorithm
from BigDataWeb.view import put_algorithm


def appstore_crawler(request):
    context = {}
    algorithm = AppstoreCrawler()
    algorithm.user_id = request.session["login_name"]
    put_algorithm(algorithm)
    context["algorithm"] = algorithm
    return render(request, "data_collector/1.html", context)


def douban_crawler(request):
    context = {}
    algorithm = DoubanCrawler()
    algorithm.user_id = request.session["login_name"]
    put_algorithm(algorithm)
    context["algorithm"] = algorithm
    return render(request, "data_collector/1.html", context)


def news_crawler(request):
    context = {}
    algorithm = NewsCrawler()
    algorithm.user_id = request.session["login_name"]
    put_algorithm(algorithm)
    context["algorithm"] = algorithm
    return render(request, "data_collector/1.html", context)


def weather_crawler(request):
    context = {}
    algorithm = WeatherCrawler()
    algorithm.user_id = request.session["login_name"]
    put_algorithm(algorithm)
    context["algorithm"] = algorithm
    return render(request, "data_collector/1.html", context)


def weibo_crawler(request):
    context = {}
    algorithm = WeiboCrawler()
    algorithm.user_id = request.session["login_name"]
    put_algorithm(algorithm)
    context["algorithm"] = algorithm
    return render(request, "data_collector/1.html", context)


def stock_crawler(request):
    context = {}
    algorithm = StockCrawler()
    algorithm.user_id = request.session["login_name"]
    put_algorithm(algorithm)
    context["algorithm"] = algorithm
    return render(request, "data_collector/1.html", context)


def configure_parameters(request):
    context = {}
    algorithm = get_algorithm(request)
    
    if algorithm.algorithm_name == "最热APP应用":
        algorithm.app_type = request.POST.get("app_type")
        
    if algorithm.algorithm_name == "豆瓣电影":
        algorithm.movie_limit_count = int(request.POST.get("movie_limit_count"))
        
    if algorithm.algorithm_name == "新闻采集":
        algorithm.news_limit_count = int(request.POST.get("news_limit_count"))
        algorithm.news_type_id = request.POST.get("news_type_id")
            
    if algorithm.algorithm_name == "历史天气":
        algorithm.month = request.POST.get("month")
        algorithm.city = request.POST.get("city")
    
    if algorithm.algorithm_name == "微博内容":
        algorithm.uid = request.POST.get("uid")
        algorithm.blog_limit_count = int(request.POST.get("blog_limit_count"))
    
    if algorithm.algorithm_name == "股票信息":
        algorithm.stock_type = request.POST.get("stock_type")
        
    algorithm.implent()
    algorithm.saveToExcle()
    algorithm.generateNotebookFile()
    context["algorithm"] = algorithm
    return render(request, "data_collector/5.html", context)
